package com.client.dbdao;

import com.admin.bean.GoodSoftwareType;
import com.data.dbdao.BaseDao;
import org.springframework.stereotype.Repository;

import javax.transaction.Transactional;
import java.util.List;

/**
 * 客户端的首页软件处理
 *
 * @author:HingLo
 * @create 2017-09-27 11:25
 **/
@Repository
@Transactional
public class IndexClientDao extends BaseDao {

    /***
     * 软件信息
     * @return
     */
    public List<?> findindexClientDao(int type) {
        String hql = "select a.id,a.name ,a.introduce,m.name,a.size,a.version,(SELECT AVG(s.star) from Speaking as s where s.rid=a.id ) as num,a.state,a.type,(SELECT d.down from AppDownload as d where d.id=a.id) as down ,a.md5,a.packagename,a.content,a.vcontent,a.ctime  from App as a ,Menu as m ,PayApp as py where py.rid=a.id and  m.id=a.menuid and a.id in (SELECT rid from PayApp WHERE type=?) order by py.id desc";
        return super.findMassageDao(hql, type);
    }

    /**
     * banner图的id查看
     *
     * @return
     */
    public List<?> findindexClientBannerDao() {
        String hql = "select id,rid,typeid from Banner";
        return super.findMassageDao(hql);
    }

    /**
     * 热榜
     *
     * @param value
     * @return
     */

    public List<?> findIndexClientDownloadDao(int param, Object... value) {
        String sql = "";
        if (param == 0) {
            sql = "select a.id,a.name ,a.introduce,m.name as mname,a.size,a.version,(SELECT AVG(s.star) from Speaking as s where s.rid=a.id ) as num,a.state,a.type,(SELECT d.down from AppDownload as d where d.id=a.id) as down, a.md5,a.packagename, a.content,a.vcontent,a.ctime  from App as a, Menu as m where  m.id=a.menuid and a.state=? and a.type=?  ORDER BY down DESC LIMIT ?,?";
        } else {
            sql = "select a.id,a.name ,a.introduce,m.name as mname,a.size,a.version,(SELECT AVG(s.star) from Speaking as s where s.rid=a.id ) as num,a.state,a.type,(SELECT d.down from AppDownload as d where d.id=a.id) as down ,a.md5,a.packagename,a.content,a.vcontent ,a.ctime  from App as a, Menu as m where  m.id=a.menuid and a.state=? and a.type=?  ORDER BY a.ctime DESC LIMIT ?,?";

        }

        return super.findMysqlMethodDao(sql, value);
    }

    /**
     * 查询精选类别信息
     *
     * @return
     */
    public List<?> findGoodSoftwareDao(int type) {
        String hql = "select g.id,g.name,g.contents from GoodSoftwareType as g where g.type=?";
        return super.findMassageDao(hql, type);
    }

    public List<GoodSoftwareType> findGoodSoftwareDao(String id) {
        String hql = "from GoodSoftwareType where id=?";
        return (List<GoodSoftwareType>) super.findMassageDao(hql, id);
    }

    /**
     * 好评软件查询详细信息
     *
     * @param id
     * @return
     */
    public List<?> findGoodChoiceSoftwareDao(String id) {
        String hql = "select a.id,a.name ,a.introduce,m.name,a.size,a.version,(SELECT AVG(s.star) from Speaking as s where s.rid=a.id ) as num,(SELECT d.down from AppDownload as d where d.id=a.id) as down ,a.state,a.type,a.md5,a.packagename,a.content,a.vcontent ,a.ctime  from App as a ,Menu as m where  m.id=a.menuid and a.id in (SELECT rid from GoodSoftware WHERE gid=?)";
        return super.findMassageDao(hql, id);
    }

    /**
     * 下载量+1
     *
     * @param rid
     * @return
     */
    public boolean addSoftwareDownloadsDao(String rid) {
        String hql = "update AppDownload set down=down+1 where id=?";
        return super.updateOneMssageDao(hql, rid);
    }

    /**
     * 根据软件的id来查询软件的详细信息
     *
     * @param rid
     * @return
     */
    public List<?> indexBannerSoftwareDao(String rid) {
        String hql = "select a.id,a.name ,a.introduce,m.name,a.size,a.version,(SELECT AVG(s.star) from Speaking as s where s.rid=a.id ) as num,(SELECT d.down from AppDownload as d where d.id=a.id) as down ,a.state,a.type,a.md5,a.packagename,a.content,a.vcontent ,a.ctime  from App as a ,Menu as m where  m.id=a.menuid and a.id =?";
        return super.findMassageDao(hql, rid);
    }
}
